package com.leyou.auth.web;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.leyou.auth.config.OSSProperties;
import com.leyou.auth.dto.AliOssSignatureDTO;
import com.leyou.auth.service.AliAuthService;
import com.leyou.common.utils.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 阿里的相关授权接口
 * 
 */
@RestController
@RequestMapping("/ali")
public class AliAuthController {

    @Autowired
    private AliAuthService aliAuthService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OSSProperties prop;

    /**
     * 申请oss签名
     * @return 包含签名、图片验证策略等信息
     */
    @GetMapping("/oss/signature")
    public ResponseEntity<AliOssSignatureDTO> getAliSignature(){
        return ResponseEntity.ok(aliAuthService.getSignature());
    }

    @GetMapping("/oss/callback")
    public ResponseEntity<String> callBack(@RequestParam("filename") String filename) throws JSONException {
        System.out.println("filename = " + filename);
        if (filename==null || filename.equalsIgnoreCase("")){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        filename=prop.getHost().concat("/").concat(filename);

        // redis存 所有图片
        redisTemplate.opsForSet().add(RedisConstant.BRAND_PIC_RESOURCES,filename);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("filename",filename);
        String json = jsonObject.toString();
        return ResponseEntity.ok(json);
    }

}